﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Landscape.Business.FNH.Activities;
using Landscape.Business.FNH;
using NHibernate;
using Landscape.Business.Entities;

namespace Landscape.WebApp.Controls
{
    public partial class ActionResponseHistory : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData().DataBind();
            }
        }

        private RadGrid LoadData()
        {
            if (Request.QueryString["AID"] != null && Request.QueryString["AID"].Trim() != string.Empty)
            {
                using (var nhSession = NHSessionManager.GetManager().GetSession())
                {
                    OperationsActivities oa = new OperationsActivities(nhSession);

                    var action = oa.GetCorrectiveActionByID(Guid.Parse(Request.QueryString["AID"].Trim()));
                    if (action != null)
                    {
                        if (action.Responses != null && action.Responses.Count() > 1)
                        {
                            var data = from resp in action.Responses
                                       where resp.IsResponseReleased == true
                                       orderby resp.ResponseIndex descending
                                       select new
                                       {
                                           resp.ID,
                                           resp.Response,
                                           resp.ResponseIndex,
                                           ResponseDate = resp.ResponseDate.ToString("dd-MMM-yyyy"),
                                           ResponseBy = GetFullName(resp.ResponseBy),
                                           ReviewDate = (resp.ReviewDate != null && resp.ReviewDate != DateTime.MinValue ? resp.ReviewDate.ToString("dd-MMM-yyyy") : "-"),
                                           resp.ReviewerComments,
                                           ReviewStatus = (resp.ReviewStatus != null && resp.ReviewStatus.Trim() != string.Empty ? resp.ReviewStatus : "-"),
                                           ReviewBy = GetFullName(resp.ReviewCommentsBy),
                                           resp.HseqComments,
                                           HseqApprovalDate = (resp.HseqApprovalDate != null && resp.HseqApprovalDate != DateTime.MinValue ? resp.HseqApprovalDate.ToString("dd-MMM-yyyy") : "-"),
                                           HseqApprovalStatus = (resp.HseqApprovalStatus != null && resp.HseqApprovalStatus.Trim() != string.Empty ? resp.HseqApprovalStatus : "-"),
                                           HseqCommentsBy = GetFullName(resp.HseqCommentsBy),
                                           resp.OdComments,
                                           OdApprovalDate = (resp.OdApprovalDate != null && resp.OdApprovalDate != DateTime.MinValue ? resp.OdApprovalDate.ToString("dd-MMM-yyyy") : "-"),
                                           OdApprovalStatus = (resp.OdApprovalStatus != null && resp.OdApprovalStatus.Trim() != string.Empty ? resp.OdApprovalStatus : "-"),
                                           OdCommentsBy = GetFullName(resp.OdCommentsBy),
                                           FilesList = GetFileListHTML(resp.Attachments)
                                       };
                            var listData = data.ToList();
                            listData.OrderBy(exp => exp.ResponseIndex);
                            listData.RemoveAt(0);
                            listData.Reverse();
                            this.RadGrid1.DataSource = listData;
                        }
                    }
                }
            }
            else
            {
                this.RadGrid1.DataSource = new object[] { };
            }

            return this.RadGrid1;
        }

        private string GetFullName(Guid pUserCode)
        {
            string fullname = string.Empty;

            if (pUserCode != null)
            {
                AdminActivities aa = new AdminActivities();
                var userMaster = aa.GetUserMasterByID(pUserCode);
                if (userMaster != null)
                {
                    fullname = userMaster.FirstName + " " + userMaster.LastName;
                }
            }
            return fullname;
        }

        private string GetFileListHTML(IList<FileAttachment> pAttachements)
        {
            string html = "<ul>{0}</ul>";

            if (pAttachements != null && pAttachements.Count > 0)
            {
                string fullList = string.Empty;
                string fileItem = "<li><a href='javaScript:ARH_DownloadFile(\"{1}\")'>{0}</a></li>";
                foreach (var item in pAttachements)
                {
                    fullList += string.Format(fileItem, item.Title, item.RepositoryCode);
                }

                html = string.Format(html, fullList);
            }
            else
            {
                html = string.Format(html, "None");
            }

            return html;
        }

        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            LoadData();
        }
    }
}